#include <D3DX10.h>
#include "Texture2DDX10.h"

mBool CTexture2DDX10::CreateFromFile( mString a_sFileName )
{
    HRESULT hr;
    ID3D10Resource *pRes;
    D3DX10CreateTextureFromFileA(g_D3DDevice.pD3D10Device, a_sFileName.c_str(), NULL, NULL, &pRes, &hr);
    pRes->QueryInterface(__uuidof(ID3D10Texture2D), (void**)&m_pTexture);
    SAFE_RELEASE(pRes);
    D3D10_TEXTURE2D_DESC TextureDesc;
    m_pTexture->GetDesc(&TextureDesc);
    if (hr != S_OK)
        return false;
    m_uHeight = TextureDesc.Height;
    m_uWidth = TextureDesc.Width;
    D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
    SRVDesc.Texture2D.MostDetailedMip = 0;
    SRVDesc.Texture2D.MipLevels       = TextureDesc.MipLevels;
    SRVDesc.Format                    = TextureDesc.Format;
    SRVDesc.ViewDimension             = D3D10_SRV_DIMENSION_TEXTURE2D;
    hr = g_D3DDevice.pD3D10Device->CreateShaderResourceView(m_pTexture, &SRVDesc, &m_pTextureSRV);
    return (hr == S_OK);
}

mBool CTexture2DDX10::Create( mUInt a_uWidth, mUInt a_uHeight, TImageFormat a_ImageFmt )
{
    HRESULT hr;
    D3D10_TEXTURE2D_DESC TextureDesc;
    ZeroMemory(&TextureDesc, sizeof(TextureDesc));
    TextureDesc.Width            = a_uWidth;
    TextureDesc.Height           = a_uHeight;
    TextureDesc.MipLevels        = 1;
    TextureDesc.ArraySize        = 1;
    TextureDesc.Format           = DX10ImageFmt(a_ImageFmt);
    TextureDesc.Usage            = D3D10_USAGE_DEFAULT;
    TextureDesc.BindFlags        = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
    TextureDesc.SampleDesc.Count = 1;
    hr = g_D3DDevice.pD3D10Device->CreateTexture2D(&TextureDesc, NULL, &m_pTexture);
    if (hr != S_OK)
        return false;
    D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
    SRVDesc.Texture2D.MostDetailedMip = 0;
    SRVDesc.Texture2D.MipLevels       = TextureDesc.MipLevels;
    SRVDesc.Format                    = TextureDesc.Format;
    SRVDesc.ViewDimension             = D3D10_SRV_DIMENSION_TEXTURE2D;
    hr = g_D3DDevice.pD3D10Device->CreateShaderResourceView(m_pTexture, &SRVDesc, &m_pTextureSRV);
    m_uHeight  = a_uHeight;
    m_uWidth   = a_uWidth;
    m_ImageFmt = a_ImageFmt;
    return (hr == S_OK);
}

ID3D10Texture2D *CTexture2DDX10::Texture( )
{
    return m_pTexture;
}

ID3D10ShaderResourceView *CTexture2DDX10::TextureSRV( )
{
    return m_pTextureSRV;
}

CTexture2DDX10::~CTexture2DDX10( )
{
    SAFE_RELEASE(m_pTexture);
    SAFE_RELEASE(m_pTextureSRV);
}

